package com.yd.app.mapper;

/**
 * @author zy
 * @create 2018-03-07 11:19
 * @desc 权限的mapper
 **/

import com.yd.app.mapper.provider.PermissionProvider;
import com.yd.domain.sys.Perm;
import com.yd.domain.searchParam.PermParam;
import com.yd.domain.sys.Role;
import com.yd.domain.sys.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PermissionMapper {
    /**
     * @Author: zy
     * @Desc:新增权限
     * @Date:  2018/3/7
     **/
    @Insert("INSERT INTO sys_perm (`code`,title,des) VALUES(#{per.code},#{per.title},#{per.des})")
    void addPer(@Param("per")Perm per);
    /**
     * @Author: zy
     * @Desc:删除权限
     * @Date:  2018/3/7
     */
    @Delete("DELETE FROM sys_perm WHERE `code` = #{code}")
    void deletePerByCode(@Param("code") String code);
    /**
     * @Author: zy
     * @Desc:权限的修改
     * @Date:  2018/3/7
     */
    @Update("UPDATE sys_perm set `code` = #{per.code},title=#{per.title},des=#{per.des} where `code`=#{per.code}")
    void updatePer(@Param("per") Perm per);
    /**
     * @Author: zy
     * @Desc:根据ID查询权限
     * @Date:  2018/3/7
     */
    @Select("select `code`,title,des from sys_perm where `code` = #{code}")
    Perm getPermByID(@Param("code") String code);
    /**
     * @Author: zy
     * @Desc:权限的分页查询数据
     * @Date:  2018/3/7
     */
    @SelectProvider(method = "searchPermList",type= PermissionProvider.class)
    List<Perm> searchPermList(@Param("permParam") PermParam permParam, @Param("startRow") int startRow, @Param("pageSize") int pageSize);
    /**
     * @Author: zy
     * @Desc:权限查询数量
     * @Date:  2018/3/7
     */
    @SelectProvider(method = "searchPermCount",type= PermissionProvider.class)
    int searchPermCount(@Param("permParam") PermParam permParam);
    /**
     * 根据角色获取权限列表
     * @param roleCode
     * @return
     */
    @Select("select sp.* from sys_role_perm srp join sys_perm sp on srp.permCode=sp.code where srp.roleId=#{roleCode} ")
    List<Perm> getPermListByRoleCode(@Param("roleCode") String roleCode);

    /**
     * 获取所有权限
     * @return
     */
    @Select(" select * from sys_perm ")
    List<Perm> getAllPermList();

    /**
     * 向角色添加权限
     * @return
     */
    @Insert("insert into sys_role_perm (roleId,permCode) values(#{roleCode},#{permCode}) ON DUPLICATE KEY UPDATE `permCode`=values(permCode) ")
    void addPermToRole(@Param("roleCode") String roleCode,@Param("permCode") String permCode);

    /**
     * 删除角色权限
     * @param roleCode
     * @param permId
     */
    @Delete("delete from sys_role_perm where roleId=#{roleCode} and permCode=#{permId}")
    void deletePermToRole(@Param("roleCode") String roleCode,@Param("permId") String permId);

    /**
     * 获取用户角色
     * @param userId
     * @return
     */
    @Select("select sr.* from sys_user_role sur left join sys_role sr on sur.roleCode=sr.code where  sur.userId=#{userId}  ")
    List<Role> getUserRoleList(@Param("userId") String userId);

    /**
     * 根据权限获取用户
     * @param permCode
     * @return
     */
    @Select("SELECT u.* from sys_user u where u.id in (SELECT DISTINCT ur.userId from sys_user_role ur  where ur.roleCode in (SELECT DISTINCT rp.roleId from sys_role_perm rp where rp.permCode=#{permCode}))")
    List<User> getUserByPermCode(@Param("permCode") String permCode);
}
